package com.tianruan.dc.modules.sys.user.api;

import com.tianruan.dc.common.utils.response.VResponse;
import com.tianruan.dc.modules.sys.user.entity.db.ConsultationDO;
import com.tianruan.dc.modules.sys.user.entity.dto.ConsultationDTO;
import com.tianruan.dc.modules.sys.user.entity.dto.TutorSolutionDTO;
import com.tianruan.dc.modules.sys.user.service.IConsultationService;
import com.tianruan.dc.modules.sys.user.utils.PageBean;
import com.tianruan.dc.modules.userinfo.entity.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * 技术咨询
 */

@RestController
@RequestMapping("/api/sys/consultation")
@Slf4j

public class ConsultationController {

    @Autowired
    private IConsultationService iConsultationService;

    /**
     * 新增查询
     * @param consultationDTO
     * @return
     */
    @PostMapping("/add{id}")
    //授权
    @PreAuthorize("hasAnyAuthority('sys:super:admin','sys:teacher','sys:student')")
    public VResponse save(@Validated @RequestBody ConsultationDTO consultationDTO) {
        log.info("新增咨询: {}",consultationDTO);
        iConsultationService.save(consultationDTO);
        return VResponse.success("已新增咨询");
    }

    /**
     * 删除咨询
     * @param id
     * @return
     */
    @DeleteMapping("/delete")
    //授权
    @PreAuthorize("hasAnyAuthority('sys:super:admin','sys:teacher','sys:student')")
    public VResponse delete(ConsultationDO consultationDO, String id) {
        log.info("删除咨询");
        iConsultationService.delete(consultationDO, id);
        return VResponse.success("已删除咨询");
    }

    /**
     * 根据id查询咨询
     * @param id
     * @return
     */
    @GetMapping("/getBy{id}")
    //授权
    @PreAuthorize("hasAnyAuthority('sys:super:admin','sys:teacher','sys:student')")
    public VResponse<ConsultationDTO> getById(String id) {
        log.info("根据id查询咨询: {}",id);
        ConsultationDTO consultationDTO = iConsultationService.getById(id);
        return VResponse.success(consultationDTO);
    }

    /**
     * 修改咨询
     * @param consultationDTO
     * @return
     */
    @PutMapping("/update")
    //授权
    @PreAuthorize("hasAnyAuthority('sys:super:admin','sys:teacher','sys:student')")
    public VResponse update(@RequestBody ConsultationDTO consultationDTO) {
        log.info("修改咨询: {}",consultationDTO);
        iConsultationService.update(consultationDTO);
        return VResponse.success("已修改咨询");
    }

    /**
     * 分页查询咨询
     * @param
     * @return
     */
    @GetMapping("/page")
    //授权
    @PreAuthorize("hasAnyAuthority('sys:super:admin','sys:teacher','sys:student')")
    public VResponse<PageBean<ConsultationDTO>> page(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10")  Integer pageSize,
            @RequestParam(required = false) String question,
            @RequestParam(required = false) String project_brief,
            @RequestParam(required = false) String create_by,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date startTime_C,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date endTime_C
    ){
        log.info("分页查询咨询:{}",startTime_C,endTime_C);
        PageBean<ConsultationDTO> list = iConsultationService.pageConsultation(page, pageSize, project_brief, question, create_by, startTime_C, endTime_C);
        return VResponse.success(list);
    }

    /**
     * 分页查询解答记录
     * @return
     */
    @GetMapping("/select")
    //授权
    @PreAuthorize("hasAnyAuthority('sys:super:admin','sys:teacher','sys:student')")
    public VResponse<PageBean<TutorSolutionDTO>> select(
            @RequestParam(defaultValue = "1")  Integer page,
            @RequestParam(defaultValue = "10")  Integer pageSize,
            @RequestParam(required = false) String project_brief,
            @RequestParam(required = false) String create_by,
            @RequestParam(required = false) String createBy,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date startTime_C,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date endTime_C,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date startTime_S,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date endTime_S
    ){
        log.info("分页查询解答记录");
        PageBean<TutorSolutionDTO> list = iConsultationService.pageSolution(page, pageSize, project_brief, create_by, createBy, startTime_C, endTime_C, startTime_S, endTime_S);
        return VResponse.success(list);
    }

    /**
     * 根据id查询导师
     * @param id
     * @return
     */
    @GetMapping("/teacher{id}")
    //授权
    @PreAuthorize("hasAnyAuthority('sys:super:admin','sys:teacher','sys:student')")
    public VResponse<User> getTutorById(String id) {
        log.info("根据id查询导师: {}",id);
        User tutor = iConsultationService.getTutorById(id);
        return VResponse.success(tutor);
    }

    /**
     * 根据用户id查询个人咨询与解答信息
     * @param user_name
     * @return
     */
    @GetMapping("/username{user_name}")
    //授权
    @PreAuthorize("hasAnyAuthority('sys:super:admin','sys:teacher','sys:student')")
    public VResponse<List<ConsultationDTO>> getByUserId(String user_name){
        log.info("根据用户id查询个人咨询与解答信息: {}",user_name);
        List<ConsultationDTO> personage = iConsultationService.getByUserId(user_name);
        return VResponse.success(personage);
    }
}
